GetVoucherByCodeQueryHandler.execute   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 13
rs 9.8
c 0
b 0
f 0
cc 2
1
import { QueryHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { IVoucherRepository } from 'src/Domain/School/Repository/IVoucherRepository';
4
import { VoucherNotFoundException } from 'src/Domain/School/Exception/VoucherNotFoundException';
5
import { GetVoucherByCodeQuery } from './GetVoucherByCodeQuery';
6
import { VoucherView } from '../../View/VoucherView';
7
8
@QueryHandler(GetVoucherByCodeQuery)
9
export class GetVoucherByCodeQueryHandler {
10
  constructor(
11
    @Inject('IVoucherRepository')
12
    private readonly voucherRepository: IVoucherRepository
13
  ) {}
14
15
  public async execute({ code }: GetVoucherByCodeQuery): Promise<VoucherView> {
16
    const voucher = await this.voucherRepository.findOneByCode(code);
17
18
    if (!voucher) {
19
      throw new VoucherNotFoundException();
20
    }
21
22
    return new VoucherView(
23
      voucher.getId(),
24
      voucher.getCode(),
25
      voucher.getEmail(),
26
      voucher.getSchool().getId(),
27
    );
28
  }
29
}
30